package top.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import top.api.Constants.SystemConstants;
import top.api.common.ResponseResult;
import top.api.domain.dto.TagDto;
import top.api.domain.entity.Tag;
import top.api.domain.vo.PageVo;
import top.api.domain.vo.TagListVo;
import top.api.enums.AppHttpCodeEnum;
import top.api.service.TagService;
import top.api.mapper.TagMapper;
import org.springframework.stereotype.Service;
import top.api.utils.BeanCopyUtils;

import java.util.List;

/**
* @author 27465
* @description 针对表【sg_tag(标签)】的数据库操作Service实现
* @createDate 2023-02-05 19:40:26
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{

    @Autowired
    private TagMapper tagMapper;

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagDto tagDto) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(tagDto.getName()),Tag::getName,tagDto.getName());
        queryWrapper.like(StringUtils.hasText(tagDto.getRemark()),Tag::getRemark,tagDto.getRemark());

        Page<Tag> pageInfo = new Page<>(pageNum,pageSize);
        page(pageInfo,queryWrapper);

        List<TagListVo> tagListVos = BeanCopyUtils.copyBeanList(pageInfo.getRecords(), TagListVo.class);

        PageVo pageVo= new PageVo(tagListVos,pageInfo.getTotal());

        return ResponseResult.okResult(pageVo);

    }

    @Override
    public ResponseResult addTag(TagDto tagDto) {
        if (! StringUtils.hasText(tagDto.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        // 查询是否存在改标签
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName,tagDto.getName());
        Integer count = tagMapper.selectCount(queryWrapper);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.TAG_EXIST);
        }


        tagDto.setId(null);

        Tag tag = BeanCopyUtils.copyBean(tagDto, Tag.class);

        tagMapper.insert(tag);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTagById(Long id) {
        LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.set(Tag::getDelFlag, SystemConstants.DelFlagNO);
        updateWrapper.eq(Tag::getId,id);
        update(updateWrapper);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTag(Long id) {
        Tag tag = tagMapper.selectById(id);

        TagListVo tagListVo = BeanCopyUtils.copyBean(tag, TagListVo.class);

        return ResponseResult.okResult(tagListVo);
    }

    @Override
    public ResponseResult updateTag(TagDto tagDto) {
        if (! StringUtils.hasText(tagDto.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        Tag tag = BeanCopyUtils.copyBean(tagDto, Tag.class);
        if (! ObjectUtils.isEmpty(tagDto.getId())){
            tagMapper.updateById(tag);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getListAllTag() {
        List<Tag> tags = tagMapper.selectList(null);
        return ResponseResult.okResult(tags);
    }
}




